/*
 * @lc app=leetcode.cn id=257 lang=cpp
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  vector<string> binaryTreePaths(TreeNode* root)
  {
    string path = to_string(root->val);
    if (root->left == nullptr && root->right == nullptr)
      return {path};
    vector<string> ans;
    if (root->left)
      DFS(ans, path, root->left);
    if (root->right)
      DFS(ans, path, root->right);
    return ans;
  }

  void DFS(vector<string>& ans, string path, TreeNode* root)
  {
    path += "->" + to_string(root->val);
    // 叶子结点
    if (root->left == nullptr && root->right == nullptr)
      ans.push_back(path);
    else {
      if (root->left)
        DFS(ans, path, root->left);
      if (root->right)
        DFS(ans, path, root->right);
    }
  }
};
// @lc code=end
